package cn.cheegon.blog;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.junit.jupiter.api.Test;

import java.nio.file.Paths;

/**
 * @Desc:
 * @Author: cheegon
 * @Date: Create in 2024/6/11  下午4:30
 */

public class CodeGenerator {

    private static String url = "jdbc:mysql://47.100.123.137:3306/test_blog?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT";

    private static String username = "test";

    private static String password = "RcSd2]w9PG1*3Zo[/x_&T=Enb";

    public static void main(String[] args) {

    }

    /**
     * 生成全部表,若存在,则跳过
     */
    @Test
    public void generatorAll() {
        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> builder
                        .author("cheegon")// 设置作者
                        .disableOpenDir() // 不允许自动打开输出目录
                        .enableSwagger() // 开启 swagger 模式
                        .outputDir(Paths.get(System.getProperty("user.dir")) + "/src/main/java")// 输出目录 获取项目的绝对路径+src/main/java
                        .commentDate("yyyy-MM-dd")//设置注释日期格式,默认值: yyyy-MM-dd
                )
                .packageConfig(builder -> builder
                        .parent("cn.cheegon.blog")// 设置父包名
                        .entity("model")// 设置实体类包名.entity("model")
                        .mapper("mapper")// 设置 Mapper 接口包名.mapper("dao")
                        .service("service") // 设置 Service 接口包名
                        .serviceImpl("service.impl")// 设置 Service 实现类包名
                        .xml("mapper.xml")// 设置 Mapper XML 文件包名
                )
                .strategyConfig(builder -> builder
                        .entityBuilder()
                        .enableTableFieldAnnotation() // 启用字段注解
                        .enableLombok()// 启用 Lombok
                        .enableFileOverride() // 启用覆盖

                )
                .strategyConfig(builder -> builder
                                .serviceBuilder()
                                .formatServiceFileName("%sService")
                                .formatServiceImplFileName("%sServiceImpl")
                        // .enableFileOverride()// 启用覆盖

                )
                .strategyConfig(builder -> builder
                                .mapperBuilder()
                        // .enableFileOverride()// 启用覆盖
                )
                .strategyConfig(builder -> builder
                                .controllerBuilder()
                        // .enableFileOverride()// 启用覆盖
                )
                .templateEngine(new FreemarkerTemplateEngine())// 使用 Freemarker 模板引擎
                .execute();
    }

    @Test
    public void generatorSingle() {
        String table = "";
        this.generatorSingle(table);
    }

    /**
     * 生成单张表,若存在,则跳过
     */
    private void generatorSingle(String table) {
        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> builder
                        .author("cheegon")
                        .enableSwagger()
                        .outputDir(Paths.get(System.getProperty("user.dir")) + "/src/main/java")
                        .commentDate("yyyy-MM-dd")
                        .disableOpenDir()
                )
                .packageConfig(builder -> builder
                        .parent("cn.cheegon.blog")
                        .entity("model")
                        .mapper("mapper")
                        .service("service")
                        .serviceImpl("service.impl")
                        .xml("mapper.xml")
                )
                .strategyConfig(builder -> builder
                        .addInclude(table) // 设置需要生成的表名
                )
                .strategyConfig(builder -> builder
                        .entityBuilder()
                        .enableFileOverride() // 启用覆盖
                        .enableTableFieldAnnotation() // 启用字段注解
                        .enableLombok()// 启用 Lombok
                )
                .strategyConfig(builder -> builder
                        .serviceBuilder()
                        .formatServiceFileName("%sService")
                        .formatServiceImplFileName("%sServiceImpl")
                )
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }

}
